package com.lczyfy.idcp.common.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lczyfy.idcp.common.persistence.Page;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保存序列号json的时候，如果是null对象，key也会消失
public class ServerResponse<T> implements Serializable {
    private int msgCode; //请求返回状态码
    private String errMsg; //附带信息
    private List<String> type; //下拉框的查询接口
    private T data; //详情和明细的接口类
    private List<T> dataList; //没有分页的集合类
    private Page<T> page; //有分页的集合类
    private String createDate; //客户端请求接口服务端时间
    private String responseDate;//服务端接收请求时间
    private String returnDate;//服务端处理完请求时间
    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }
    public String getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(String returnDate) {
        this.returnDate = returnDate;
    }

    private ServerResponse(int msgCode) {
        this.msgCode = msgCode;
    }
    public List<String> getType() {
        return type;
    }

    public void setType(List<String> type) {
        this.type = type;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }

    public String getResponseDate() {
        return responseDate;
    }

    public void setResponseDate(String responseDate) {
        this.responseDate = responseDate;
    }
    public ServerResponse(int msgCode, T data) {
        this.msgCode = msgCode;
        this.errMsg = MsgCodeUtils.getErrMsg(msgCode);
        this.data = data;
    }
    public Page<T> getPage() {
        return page;
    }

    public void setPage(Page<T> page) {
        this.page = page;
    }
    public ServerResponse(int msgCode, String errMsg) {
        this.msgCode = msgCode;
        this.errMsg =  errMsg;
    }
    public ServerResponse(int msgCode, Page<T> page) {
        this.errMsg = MsgCodeUtils.getErrMsg(msgCode);
        this.msgCode = msgCode;
        this.page= page;
    }
    public ServerResponse(int msgCode, List<T> dataList) {
        this.msgCode = msgCode;
        this.errMsg = MsgCodeUtils.getErrMsg(msgCode);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.responseDate = df.format(new Date());
        this.dataList= dataList;
    }
    public ServerResponse(String createDate, int msgCode, List<T> dataList) {
        this.msgCode = msgCode;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        this.returnDate = df.format(new Date());
        this.dataList= dataList;
        this.responseDate = createDate;
    }
    public ServerResponse(List<String> type) {
        this.type = type;
    }
    public ServerResponse(int msgCode, String  errMsg, List<T> dataList) {
        this.msgCode = msgCode;
        this.errMsg=  errMsg;
        this.dataList= dataList;
    }
    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess() {
        return this.msgCode == ResponseCode.SUCCESS.getCode();
    }
    public int getMsgCode() {
        return this.msgCode;
    }

    public String geterrMsg() {
        return this. errMsg;
    }

    public T getData() {
        return this.data;
    }
    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
        return new ServerResponse<T>(ResponseCode.CREATE_SUCCESS.getCode(), msg);
    }
    public static <T> ServerResponse<T> deleteBySuccessMessage(String msg) {
        return new ServerResponse<T>(ResponseCode.DELETE_SUCCESS.getCode(), msg);
    }
    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }
    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }
    public static <T> ServerResponse<T> unLogin() {    //未登陆
        String errorMessage=MsgCodeUtils.getErrMsg(ResponseCode.UN_LOGIN.getCode());
        return new ServerResponse<T>(ResponseCode.UN_LOGIN.getCode(), errorMessage);
    }
    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

}